﻿<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:chart="http://www.gdtti.gov.mo/chart"
      exclude-result-prefixes="chart msxsl">

<msxsl:script language="JavaScript" implements-prefix="chart">
<![CDATA[
function Chart(legends, dataImg)
{
  this.legends = legends;
  this.dataImg = dataImg; 
  this.maxValues = new ChartValue(0, 0, new Array(legends.length));
  this.minValues = new ChartValue(0, 0, new Array(legends.length));
  for (var i=0; i<this.maxValues.stats.length; i++)
  {
     this.maxValues.stats[i] = 0;
     this.minValues.stats[i] = 9999999;
  }
  this.chartValues = new Array();
  this.addChartValue = addChartValue;
  this.displayChart = displayChart;
  this.getBarHTML = getBarHTML;
  this.getChartHTML = getChartHTML;
}

function ChartValue(month, year, stats)
{
  this.month = month;
  this.year = year;
  if (stats && stats.length)
    this.stats = stats;
  else
  {
    this.stats = new Array(1);
    this.stats[0] = stats;
  } 
}

function addChartValue(chartValue)
{
   var n = this.chartValues.length;
   if (chartValue.stats.length!=this.legends.length)
    alert("Invalid chartValue");

   this.chartValues[n] = chartValue;
   for (var i=0; i<chartValue.stats.length; i++)
   {
     if (this.maxValues.stats[i]<chartValue.stats[i]) 
      this.maxValues.stats[i] = chartValue.stats[i];
     if (this.minValues.stats[i]>chartValue.stats[i]) 
      this.minValues.stats[i] = chartValue.stats[i];
   }
}

function nicenum(x,round) 
{
  var expt = Math.floor(Math.log(x)/Math.E);
  var f = x/Math.pow(10,expt);

  var nf;
  if(round == 1) {
    if (f< 1.5) nf = 1.;
    else if (f<3) nf = 2.;
    else if (f<7) nf = 5.;
    else nf = 10.;
  } else {
    if (f<= 1) nf = 1.;
    else if (f<=2) nf = 2.;
    else if (f<=5) nf = 5.;
    else nf = 10.;
  }

  return nf*Math.pow(10, expt);
}

function textPercentage(percentage)
{
   var c = Math.round(percentage/100*70);
   var s="";
   for (var i=0; i<c; i++)
	s+="*"; 
   return s;
}

function getBarHTML(chartValue, statIndex, min, max)
{
  var mx;
  var mn;
  if (typeof(min)!='undefined')
     mn=min;
  else
     mn = this.minValues.stats[statIndex];
  if (typeof(max)!='undefined')
  {
	if (this.maxValues.stats[0]<max)
	   mx = max;
        else
      	   mx = this.maxValues.stats[0];
  } 
  else
	mx = this.maxValues.stats[statIndex];
  //var nmx = (mx - 0.0)+(mx - mn)*0.15;
  //var nmn = mn - (mx - mn)*0.15; 
  var exp = Math.floor(Math.log(mx)/Math.LN10);
  var unit = Math.pow(10,exp);
  var nmx = Math.ceil(mx/unit)*unit;
  var nmn = Math.floor(mn/unit)*unit;

  var curr = chartValue.stats[statIndex];
  var percentage;
  if (nmx==nmn)
	percentage = 100;
  else
  	percentage = (curr-nmn)/(nmx-nmn)*100;  
  
  var s="<TD WIDTH='80%'>";
  //s+=nmn+","+nmx+"<BR/>";
  /*document.writeln("<HR ALIGN='LEFT' COLOR='#00ff00' WIDTH='"+
    Math.round(percentage)+"%' SIZE='10'>");*/
  s+="<IMG SRC='"+this.dataImg+"' WIDTH='"+Math.round(percentage)+"%' HEIGHT='10' ALT='"+
     textPercentage(percentage)+"' />";
  s+="</TD>";  
  s+="<TD>"+curr+"</TD>";
  return s;
}

function getChartHTML(min, max)
{
   var s = "<TABLE BORDER='0' WIDTH='80%'>\n";
   for (var i=0; i<this.chartValues.length; i++)
   {
     var chartValue = this.chartValues[i];
     for (var j=0; j<chartValue.stats.length; j++)
     {
       s = s+ "<TR>\n";      
       if (j==0)
         s+="<TH ROWSPAN='"+chartValue.stats.length+"' WIDTH='10%'>"+
          chartValue.month+"/"+chartValue.year+"</TH>\n";  
       s+=this.getBarHTML(chartValue, j, min, max); 
       s+="</TR>\n";
     }
   }

  /* Add x-axis labels */
  var mx;
  var mn;
  
  if (typeof(max)!='undefined')
  {
	if (this.maxValues.stats[0]<max)
	   mx = max;
        else
      	   mx = this.maxValues.stats[0];
  }
  else
	mx = this.maxValues.stats[0];
  if (typeof(min)!='undefined')
      mn = min;
  else
      mn = this.minValues.stats[0];
   //var nmx = Math.round((mx - 0.0)+(mx - mn)*0.15);
   //var nmn = Math.round(mn - (mx - mn)*0.15); 

  var exp = Math.floor(Math.log(mx)/Math.LN10);
  var unit = Math.pow(10,exp);
  var nmx = Math.ceil(mx/unit)*unit;
  var nmn = Math.floor(mn/unit)*unit;
  //s+="min:"+mn+";nmin:"+nmn;
  s+="<TR><TD></TD><TD><TABLE WIDTH='100%' BORDER='0' CELLSPACING='0' CELLPADDING='0'>"+
      "<TR><TD>"+nmn+"</TD>"+
      "<TD ALIGN='RIGHT'>"+nmx+"</TD></TR></TABLE>"+
      "</TD></TR>\n";
   s+="</TABLE>";
   return s;
}

function displayChart(min,max)
{  
  document.writeln(this.getChartHTML(min,max));
}
]]>
</msxsl:script>
</xsl:stylesheet>
